package Q661;

/**
 * 661. 图片平滑器
 */
public class Solution {

    public static int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0},{1, 1}, {-1, -1}, {-1, 1}, {1, -1}};

    public int[][] imageSmoother(int[][] img) {
        int[][] res = new int[img.length][img[0].length];
        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[i].length; j++) {

                int count = 1;
                int sum = img[i][j];

                for (int[] d : directions) {
                    int x = i + d[0];
                    int y = j + d[1];

                    if (x >= 0 && y >= 0 && x < img.length && y < img[i].length ) {
                        sum += img[x][y];
                        count++;
                    }
                }

                res[i][j] = sum / count;
            }
        }
        return res;
    }


    public static void main(String[] args) {
        int[][] input = {{1,1,1},{1,0,1},{1,1,1}};
        int[][] res = new Solution().imageSmoother(input);
        System.out.println(res);
    }
}
